package com.xzssmk.leisure.x.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzssmk.leisure.x.mapper.CardTypeMapper;
import com.xzssmk.leisure.x.mapper.IssuePeriodMapper;
import com.xzssmk.leisure.x.po.IssuePeriod;
import com.xzssmk.leisure.x.po.IssuePeriodWithBLOBs;
import com.xzssmk.leisure.x.po.SelectModel;
import com.xzssmk.leisure.x.service.BaseService;
import com.xzssmk.leisure.x.service.IssuePeriodService;
import com.xzssmk.leisure.x.util.DataTables;
import com.xzssmk.leisure.x.util.RequestStatusUtil;

@Service
public class IssuePeriodServiceImpl extends BaseService implements IssuePeriodService {

    @Autowired
    private IssuePeriodMapper issuePeriodMapper;

    @Override
    public Map<String, Object> add(IssuePeriodWithBLOBs issuePeriodWithBLOBs) {
        Map<String, Object> map = new HashMap<>();
        Integer id = issuePeriodMapper.insert(issuePeriodWithBLOBs);
        if (id > 0) {
            map.put("status", RequestStatusUtil.SUCCESS);
        } else {
            map.put("status", RequestStatusUtil.FAILURE);
        }
        return map;
    }

    @Override
    public Map<String, Object> delete(List<Integer> idlist) {
        Map<String, Object> map = new HashMap<>();
        if (issuePeriodMapper.deleteByPrimaryKeys(idlist) > 0) {
            map.put("status", RequestStatusUtil.SUCCESS);
        } else {
            map.put("status", RequestStatusUtil.FAILURE);
        }
        return map;
    }

    @Override
    public Map<String, Object> update(IssuePeriodWithBLOBs issuePeriodWithBLOBs) {
        Map<String, Object> map = new HashMap<>();
        if (issuePeriodMapper.updateByPrimaryKeySelective(issuePeriodWithBLOBs) > 0) {
            map.put("status", RequestStatusUtil.SUCCESS);
        } else {
            map.put("status", RequestStatusUtil.FAILURE);
        }
        return map;
    }

    @Override
    public DataTables getPageList(DataTables dataTables) {
        PageHelper.startPage(dataTables.getStart(), dataTables.getLength()); // 核心分页代码
        PageHelper.orderBy("modify_time desc");
        // https: // blog.csdn.net/lvbang_lzt/article/details/54375091
        // https: // blog.csdn.net/evane1890/article/details/63262610
        // https: // blog.csdn.net/qq_33315102/article/details/80572566
        if (!StringUtils.isEmpty(dataTables.getColumn())) {
            String column = propertyToColumn(CardTypeMapper.class, dataTables.getColumn());
            if (!StringUtils.isEmpty(column)) {
                PageHelper.orderBy(column + " " + dataTables.getOrder());
            }
        }
        List<IssuePeriod> listIssuePeriod = issuePeriodMapper.getPageList(dataTables.getSubSQL());
        PageInfo<IssuePeriod> pageInfo = new PageInfo<IssuePeriod>(listIssuePeriod);
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ? pageInfo.getList() : new ArrayList<>());

        return dataTables;
    }

    @Override
    public IssuePeriodWithBLOBs getById(Integer id) {
        return issuePeriodMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SelectModel> getSelectList() {
        return issuePeriodMapper.getSelectList();
    }

    @Override
    public Map<String, Object> updatePublicState(Integer issuePeriodId, Integer publicState) {

        Map<String, Object> map = new HashMap<>();
        if (issuePeriodMapper.updatePublicState(issuePeriodId, publicState) > 0) {
            map.put("status", RequestStatusUtil.SUCCESS);
        } else {
            map.put("status", RequestStatusUtil.FAILURE);
        }
        return map;
    }

}
